<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.DriverManager" %><%--
  Created by IntelliJ IDEA.
  User: 123
  Date: 2018/3/20
  Time: 15:46
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>小帅哥</title>
    <style type="text/css">
        table {
            border: 1px solid black;
            border-collapse: collapse;
        }

        td, tr {
            border: 1px solid red;
            padding: 10px;
        }

    </style>
</head>
<body>

<h1> 这是一个JSP文件</h1>

    <%
    out.write("hello,world");
    out.write("<h1>hello world<h1>");
%>


    <%
    if (session.getAttribute("isLogin")==null){

        //没有登录，跳转到login.jsp
        response.sendRedirect("longin.jsp");
    }


    %>

<table>
    <thead>
    <tr>
        <td>账号</td>
        <td>账户</td>
        <td>密码</td>

    </tr>

    </thead>

    <tbody>

    <%
        class User {
            int id;
            String name;
            String password;

            public User(int id, String name, String password) {
                this.id = id;
                this.name = name;
                this.password = password;
            }
        }

        // User chen=new User(1,"chen","123456");直接写

        List<User> list = new ArrayList<User>();
//
//        list.add(new User(1, "chen", "123456"));//从集合里面得到
//        list.add(new User(2, "zhang", "123456"));
//        list.add(new User(3, "li", "123456"));
//        list.add(new User(4, "wu", "123456"));
//
//
//        3+1

        Connection connection = null;

        Statement statement = null;

        ResultSet resultSet = null;
        String sql = "select * from t_users";

        Class.forName("com.mysql.cj.jdbc.Driver");


        connection = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/sctu_javaweb_2018?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC",
                "root",
                "123456"
        );

        statement = connection.createStatement();//从数据库里面的到

        resultSet = statement.executeQuery(sql);
        while (resultSet.next()) {
            int id = resultSet.getInt(1);
            String username = resultSet.getString(2);
            String pssword = resultSet.getString(3);


            User user = new User(id, username, pssword);
            list.add(user);
        }


        resultSet.close();
        statement.close();
        connection.close();


        for (User user : list) {
            out.write("    <tr>\n" +//alt+ctrl+l   整理代码格式化
                    "      <td>" + user.id + "</td>\n" +
                    "      <td>" + user.name + "</td>\n" +
                    "      <td>" + user.password + "</td>\n" +
                    "\n" +
                    "    </tr>");

        }
    %>

    </tbody>

    <tfoot></tfoot>

</table>

<!--静态信息>
<%--<%--%>
   <%--@ include file="footer.jsp"--%>
<%--%>--%>

//动态信息
<jsp:include page="footer.jsp">
    <jsp:param name="year" value="2018"/>

</jsp:include>

</body>
</html>
